package com.queclink.ycm.handler;

import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author jim.li
 * @email jim.li@queclink.com
 * @date 2022-01-10 09:36:00
 */
public class GatewayOutBoundHandler extends ChannelOutboundHandlerAdapter {

    private static final Logger log = LoggerFactory.getLogger(GatewayOutBoundHandler.class);

    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
        if (msg instanceof byte[]) {
            byte[] bytes = (byte[]) msg;
            ByteBuf buf = ctx.alloc().buffer(bytes.length);
            buf.writeBytes(bytes);
            ctx.writeAndFlush(buf).addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture arg0) throws Exception {
                    if (arg0.isSuccess()) {
                        System.out.println("success");
                    } else {
                        System.out.println("failed");
                    }
                }
            });
        }
    }
}
